﻿$(function(){
    var request = moon.getRequest(location.search);//获取url中"?"符后的参数
    var deviceId = request['deviceId'];//获取设备deviceId
    $("#submit").click(function(){
        $("#sensorForm").validate("validate").done(function(result){
            var sensor={
                title:$("#title").val(),
                tags:$("#tags").val(),
                type:$("#type").val(),
                unit:$("#unit").val(),
                formula:$("#formula").val(),
                period:$("#period").find("option:selected").text(),
                time:$("#time").val(),
                about:$("#about").val()
            }

            $.getJsonData(contextPath+"/sensor/"+deviceId,sensor,{type:"Post"}).done(function(data){
                console.log(data);
                if(data.success){
                    window.location.href=contextPath+"sen_suc_add.html?deviceId="+deviceId;
                }else{
                    moon.error("添加失败","top");
                }
            }).fail(function(e){
                console.log(e);
            });
        })
    })

    $("#loginOut").on("click",function(){
        $.getJsonData(fullServerPath+"/users/logout",{},{type:"Post"}).done(function(data){
            if(data.success){
                moon.info("退出","top");
                window.location.href = contextPath+"login.html";
            }else{
                moon.error("退出失败","top");
            }
        })
    })

    //添加表单验证
    $("#sensorForm").validate({align:'bottom',theme:"darkblue"});

    $(document).ready(function(){
        $("#type").bind("mouseleave",function(){
            var data = $("#type  option:selected").val();
            if(data == 2 || data==3){
                $("#unit").attr("disabled",true);
                $("#unit").attr("placeholder","此类型不需要输入");
                $("#formula").attr("disabled",true);
                $("#formula").attr("placeholder","此类型不需要输入");
            }
            if(data == 1){
                $("#unit").attr("disabled",false);
                $("#unit").attr("placeholder","摄氏度/");
                $("#formula").attr("disabled",false);
                $("#formula").attr("placeholder","支持带有括号的四则运算, v代表当前值，示例：(v + 1) * 1 + 10");
            }
        });
    })
})
